<?php
class Group extends ActiveRecord{
	public function initialize(){
		$this->source = "qo_groups";
		$this->has_many("group_has_member", "fk: qo_groups_id");
		$this->has_and_belongs_to_many("privilege", "fk: qo_privileges_id", "key: qo_groups_id", "through: qo_groups_has_privileges");
	}
	
	/**
    * getByMemberId() Retorna los groups asociados a el id de un member.
    *
    * @access public
    * @param {integer} $intMemberId Id del member.
    * @param {boolean} $bolActive (optional) True para solo retornar los groups activos.
    * @return {array}
    */
	public function getByMemberId($intMemberId, $bolActive = false){
		$strSql = "
		SELECT
		 qo_groups.*
		FROM
		 qo_members
		 INNER JOIN qo_groups_has_members ON qo_members.id = qo_groups_has_members.qo_members_id
		 INNER JOIN qo_groups ON qo_groups_has_members.qo_groups_id = qo_groups.id
		WHERE
		 qo_members.id = '$intMemberId'";
		if($bolActive){
		 $strSql .= "
		  AND qo_groups_has_members.active = '1'
		  AND qo_groups.active = '1'
		 ORDER BY
		  qo_groups.name";
		}
		 
		$arrGroups = $this->find_all_by_sql($strSql);
		if(count($arrGroups) > 0){
			return $arrGroups;
		}
		return null;
	}
}
?>